﻿#region Using Statements
using System;
using DirectZZT.Shared.Core;
using DirectZZT.Shared.World;
using DirectZZT.Shared.World.Objects;
#endregion

namespace DirectZZT.BoardObjects
{
    /// <summary>
    /// Represents the walls ("Wall") board object.
    /// </summary>
    [Export("Wall")]
    [BoardObject(Identifier = "Wall", Classification = BoardObjectClassification.Terrain)]
    public sealed class WallBoardObject : BoardObject
    {
        #region Static

        /// <summary>
        /// An array containing all allowed modifiers for this object.
        /// </summary>
        private static readonly string[] ALLOWED_MODIFIERS = { "solid", "normal", "fake", "invisible", "breakable", "blink" };


        #endregion

        #region Fields

        private WallType _wallType = WallType.Solid;
        private bool _isBroken;

        #endregion

        #region Properties

        /// <summary>
        /// Returns the capabilities for this board object.
        /// </summary>
        public override BoardObjectCapabilities Capabilities
        {
            get
            {
                BoardObjectCapabilities caps = BoardObjectCapabilities.None;

                // determine caps according to wall type
                switch (_wallType)
                {
                    case WallType.Solid:
                    case WallType.Normal:
                    case WallType.Invisible:
                    case WallType.Blink:
                        caps |= BoardObjectCapabilities.HasCollision;
                        break;
                    case WallType.Breakable:
                        if (_isBroken)
                        {
                            caps |= BoardObjectCapabilities.HasCollision;
                        } break;
                    default:
                        break;
                }
                return caps;
            }
        }


        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the WallBoardObject class.
        /// </summary>
        public WallBoardObject()
        {
            this.Texture = "Textures/PlayfieldObjects/Solid";
            this.Enabled = true;
            this.Visible = true;

            this.Tags = new string[] { "Solid", "Wall" };
            _isBroken = false;
            this.UpdateInterval = 5;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Only DirectZZT supported.
        /// </summary>
        /// <param name="featureSet"></param>
        /// <returns></returns>
        protected override bool CanAttachScript(FeatureSet featureSet)
        {
            return featureSet == FeatureSet.DirectZZT;
        }

        /// <summary>
        /// Called when this object shall spawn.
        /// </summary>
        /// <param name="modifiers">The modifiers used to spawn this object.</param>
        public override void OnSpawn(string[] modifiers)
        {
            base.OnSpawn(modifiers);

            // check if modifiers are valid
            if (modifiers != null && modifiers.Length > 0)
            {
                Enum.TryParse<WallType>(modifiers[0], true, out _wallType);
            }
        }

        #endregion

        #region Nested types

        /// <summary>
        /// Specifies the wall type that this wall board object represents.
        /// </summary>
        enum WallType
        {
            /// <summary>
            /// A solid wall.
            /// </summary>
            Solid,
            /// <summary>
            /// A normal wall????
            /// </summary>
            Normal,
            /// <summary>
            /// A fake wall (looks like a wall but you can pass through).
            /// </summary>
            Fake,
            /// <summary>
            /// An invisible wall.
            /// </summary>
            Invisible,
            /// <summary>
            /// A breakable wall.
            /// </summary>
            Breakable,
            /// <summary>
            /// A blinking wall.
            /// </summary>
            Blink,
        }

        #endregion
    }
}
